package com.tsysytems.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsystems.entities.Station;
import com.tsystems.protocol.client.ClientPassenger;

public class TTEManagementPanel extends JPanel {

	DepartmentTimeTableElementTable timeTableElementsTable = new DepartmentTimeTableElementTable();

	private static final long serialVersionUID = 6489712085149821505L;

	Logger logger = LoggerFactory.getLogger(getClass());

	public TTEManagementPanel() {
		super(new GridBagLayout());

		setBorder(new TitledBorder("Расписание"));

		timeTableElementsTable.updateTable(ClientPassenger.getInstance()
				.viewTimeTableElements());
		timeTableElementsTable.setSelectListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent e) {

			}
		});

		add(new JScrollPane(timeTableElementsTable), new GridBagConstraints(0,
				0, 0, 1, 1.0, 1.0, GridBagConstraints.CENTER,
				GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		add(createButtonPanel(), new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(-5, 0, 0, 0), 0, 0));

	}

	public JPanel createButtonPanel() {

		JPanel result = new JPanel(new FlowLayout());
		Vector<Station> tteVector = new Vector<Station>();
		tteVector.add(new Station("Все станции"));
		tteVector.addAll(ClientPassenger.getInstance().viewStations());
		JComboBox timeTableElementsBox = new JComboBox(tteVector);

		timeTableElementsBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JComboBox box = (JComboBox) e.getSource();
				Object object = box.getSelectedItem();

				if (object instanceof Station) {
					Station element = (Station) object;
					if (element.getName().equals("Все станции")) {
						timeTableElementsTable.updateTable(ClientPassenger
								.getInstance().viewTimeTableElements());

					} else {
						timeTableElementsTable.updateTable(ClientPassenger
								.getInstance().getTimeTable(element));

					}
				}

			}
		});
		result.add(new JLabel("Станция отправления:"));
		result.add(timeTableElementsBox);

		return result;
	}

	public void updateTable() {

		timeTableElementsTable.updateTable(ClientPassenger.getInstance()
				.viewTimeTableElements());

	}
}
